package cn.itcast.quartz.job;

import org.quartz.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 定义一个job
 * Job实例在Quartz中的生命周期：每次调度器执行Job时，
 * 它在调用execute方法前会创建一个新的Job实例，当调用完成后，
 * 关联的Job对象实例会被释放，释放的实例会被垃圾回收机制回收。
 */
@PersistJobDataAfterExecution
public class HelloJob implements Job {
    private long count;

    public void setCount(long count) {
        this.count = count;
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateString = LocalDateTime.now().format(formatter);
        System.out.println("执行时间" + dateString);

        JobDetail jobDetail = context.getJobDetail();
//        System.out.println(jobDetail.getJobDataMap().get("message"));

        Trigger trigger = context.getTrigger();
//        System.out.println(trigger.getJobDataMap().get("message"));


//        System.out.println(context.getFireTime());
//        System.out.println(context.getNextFireTime());

        Scheduler scheduler = context.getScheduler();
        Date scheduledFireTime = context.getScheduledFireTime();
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();

        count++;
        System.out.println(count);
        jobDetail.getJobDataMap().put("count", count);
    }
}
